<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('stock.locations.loccode')" type="string" v-model="baTable.form.items!.loccode" prop="loccode" :placeholder="t('Please input field', { field: t('stock.locations.loccode') })" />
                    <FormItem :label="t('stock.locations.locationname')" type="string" v-model="baTable.form.items!.locationname" prop="locationname" :placeholder="t('Please input field', { field: t('stock.locations.locationname') })" />
                    <FormItem :label="t('stock.locations.deladd1')" type="string" v-model="baTable.form.items!.deladd1" prop="deladd1" :placeholder="t('Please input field', { field: t('stock.locations.deladd1') })" />
                    <FormItem :label="t('stock.locations.deladd2')" type="string" v-model="baTable.form.items!.deladd2" prop="deladd2" :placeholder="t('Please input field', { field: t('stock.locations.deladd2') })" />
                    <FormItem :label="t('stock.locations.deladd3')" type="string" v-model="baTable.form.items!.deladd3" prop="deladd3" :placeholder="t('Please input field', { field: t('stock.locations.deladd3') })" />
                    <FormItem :label="t('stock.locations.deladd4')" type="string" v-model="baTable.form.items!.deladd4" prop="deladd4" :placeholder="t('Please input field', { field: t('stock.locations.deladd4') })" />
                    <FormItem :label="t('stock.locations.deladd5')" type="string" v-model="baTable.form.items!.deladd5" prop="deladd5" :placeholder="t('Please input field', { field: t('stock.locations.deladd5') })" />
                    <FormItem :label="t('stock.locations.deladd6')" type="string" v-model="baTable.form.items!.deladd6" prop="deladd6" :placeholder="t('Please input field', { field: t('stock.locations.deladd6') })" />
                    <FormItem :label="t('stock.locations.tel')" type="string" v-model="baTable.form.items!.tel" prop="tel" :placeholder="t('Please input field', { field: t('stock.locations.tel') })" />
                    <FormItem :label="t('stock.locations.fax')" type="string" v-model="baTable.form.items!.fax" prop="fax" :placeholder="t('Please input field', { field: t('stock.locations.fax') })" />
                    <FormItem :label="t('stock.locations.email')" type="string" v-model="baTable.form.items!.email" prop="email" :placeholder="t('Please input field', { field: t('stock.locations.email') })" />
                    <FormItem :label="t('stock.locations.contact')" type="string" v-model="baTable.form.items!.contact" prop="contact" :placeholder="t('Please input field', { field: t('stock.locations.contact') })" />
                    <FormItem :label="t('stock.locations.taxprovinceid')" type="remoteSelect" v-model="baTable.form.items!.taxprovinceid" prop="taxprovinceid" :input-attr="{ pk: 'cncerp_setup_taxprovinces.taxprovinceid', field: 'taxprovincename', 'remote-url': '/admin/setup.Taxprovinces/index' }" :placeholder="t('Please select field', { field: t('stock.locations.taxprovinceid') })" />
                    <FormItem :label="t('stock.locations.cashsalecustomer')" type="string" v-model="baTable.form.items!.cashsalecustomer" prop="cashsalecustomer" :placeholder="t('Please input field', { field: t('stock.locations.cashsalecustomer') })" />
                    <FormItem :label="t('stock.locations.managed')" type="number" prop="managed" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.managed" :placeholder="t('Please input field', { field: t('stock.locations.managed') })" />
                    <FormItem :label="t('stock.locations.cashsalebranch')" type="string" v-model="baTable.form.items!.cashsalebranch" prop="cashsalebranch" :placeholder="t('Please input field', { field: t('stock.locations.cashsalebranch') })" />
                    <FormItem :label="t('stock.locations.internalrequest')" type="switch" v-model="baTable.form.items!.internalrequest" prop="internalrequest" />
                    <FormItem :label="t('stock.locations.usedforwo')" type="switch" v-model="baTable.form.items!.usedforwo" prop="usedforwo" />
                    <FormItem :label="t('stock.locations.glaccountcode')" type="remoteSelect" v-model="baTable.form.items!.glaccountcode" prop="glaccountcode" :input-attr="{ pk: 'cncerp_fm_chartmaster.accountcode', field: 'accountname', 'remote-url': '/admin/fm.Chartmaster/index' }" :placeholder="t('Please select field', { field: t('stock.locations.glaccountcode') })" />
                    <FormItem :label="t('stock.locations.allowinvoicing')" type="switch" v-model="baTable.form.items!.allowinvoicing" prop="allowinvoicing" />
                    <FormItem :label="t('stock.locations.leases')" type="switch" v-model="baTable.form.items!.leases" prop="leases" />
                    <FormItem :label="t('stock.locations.lodopIp')" type="string" v-model="baTable.form.items!.lodopIp" prop="lodopIp" :placeholder="t('Please input field', { field: t('stock.locations.lodopIp') })" />
                    <FormItem :label="t('stock.locations.discount_ceiling_percentage')" type="number" prop="discount_ceiling_percentage" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.discount_ceiling_percentage" :placeholder="t('Please input field', { field: t('stock.locations.discount_ceiling_percentage') })" />
                    <FormItem :label="t('stock.locations.format_bill')" type="select" v-model="baTable.form.items!.format_bill" prop="format_bill" :data="{ content: { '0': t('stock.locations.format_bill 0'), '1': t('stock.locations.format_bill 1') } }" :placeholder="t('Please select field', { field: t('stock.locations.format_bill') })" />
                    <FormItem :label="t('stock.locations.notice_or_not')" type="switch" v-model="baTable.form.items!.notice_or_not" prop="notice_or_not" />
                    <FormItem :label="t('stock.locations.notice_type')" type="select" v-model="baTable.form.items!.notice_type" prop="notice_type" :data="{ content: {} }" :placeholder="t('Please select field', { field: t('stock.locations.notice_type') })" />
                    <FormItem :label="t('stock.locations.mobile_pay_id')" type="remoteSelect" v-model="baTable.form.items!.mobile_pay_id" prop="mobile_pay_id" :input-attr="{ pk: 'cncerp_pay_mchconfig.Id', field: 'Name', 'remote-url': '/admin/pay.Mchconfig/index' }" :placeholder="t('Please select field', { field: t('stock.locations.mobile_pay_id') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    loccode: [buildValidatorData({ name: 'required', title: t('stock.locations.loccode') })],
    locationname: [buildValidatorData({ name: 'required', title: t('stock.locations.locationname') })],
    email: [buildValidatorData({ name: 'email', title: t('stock.locations.email') })],
    taxprovinceid: [buildValidatorData({ name: 'required', title: t('stock.locations.taxprovinceid') })],
    internalrequest: [buildValidatorData({ name: 'number', title: t('stock.locations.internalrequest') })],
    usedforwo: [buildValidatorData({ name: 'number', title: t('stock.locations.usedforwo') })],
    allowinvoicing: [buildValidatorData({ name: 'number', title: t('stock.locations.allowinvoicing') })],
    leases: [buildValidatorData({ name: 'number', title: t('stock.locations.leases') })],
    lodopIp: [buildValidatorData({ name: 'url', title: t('stock.locations.lodopIp') })],
    discount_ceiling_percentage: [buildValidatorData({ name: 'number', title: t('stock.locations.discount_ceiling_percentage') })],
    notice_or_not: [buildValidatorData({ name: 'number', title: t('stock.locations.notice_or_not') })],
    notice_type: [buildValidatorData({ name: 'number', title: t('stock.locations.notice_type') })],
})
</script>

<style scoped lang="scss"></style>
